package com.hejing.方法引用与构造器引用;

import com.hejing.方法引用与构造器引用.data.Employee;
import org.junit.Test;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * ClassName: ConstructorRefTest
 * Package: com.hejing.方法引用与构造器引用
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-06-05 21:01
 * @Version: 1.0
 */
public class ConstructorRefTest {

    // 构造器引用
    // Supplier 中的 T get()
    @Test
    public void test(){

        // 1、原始接口形式
        Supplier<Employee> sup1 = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println(sup1.get());

        // 2、lambda表达式形式
        Supplier<Employee> sup2 = () -> new Employee();
        System.out.println(sup2.get());

        // 3、方法引用形式(构造器引用)
        Supplier<Employee> sup3 = Employee::new;
        System.out.println(sup3.get());


    }

    // Function中的R apply(T t)
    @Test
    public void test02(){

        // 1、原始接口调用:
        Function<Integer, Employee> func1 = new Function<Integer, Employee>() {
            @Override
            public Employee apply(Integer integer) {
                System.out.println(integer);
                return new Employee(integer);
            }
        };
        System.out.println(func1.apply(100));

        // 2、lambda表达式的形式
        Function<Integer, Employee> func2 = integer -> new Employee(integer);
        System.out.println(func2.apply(100));

        // 3、方法引用的形式(构造器引用)
        Function<Integer, Employee> func3 = Employee::new;
        System.out.println(func3.apply(100));
    }

    // BiFunction中的R apply(T t, U u)
    @Test
    public void test03(){

        // 1、原始接口的形式
        BiFunction<Integer, String, Employee> bf1 = new BiFunction<Integer, String, Employee>() {
            @Override
            public Employee apply(Integer integer, String s) {
                return new Employee(integer, s);
            }
        };
        System.out.println(bf1.apply(10, "tom"));

        // 2、lambda表达式的形式
        BiFunction<Integer, String, Employee> bf2 = (integer, str) -> new Employee(integer, str);
        System.out.println(bf2.apply(10, "tom"));

        // 3、方法引用的形式(构造器引用)
        BiFunction<Integer, String, Employee> bf3 = Employee::new;
        System.out.println(bf3.apply(10, "Tom"));
    }


    /**
     * 数组引用
     */
    // Function中的 R apply(T t)
    @Test
    public void test04(){

        // 1、原始接口的形式
        Function<Integer, Employee[]> func1 = new Function<Integer, Employee[]>() {
            @Override
            public Employee[] apply(Integer length) {
                return new Employee[length];
            }
        };
        System.out.println(func1.apply(10).length);

        // 2、lambda表达式的写法
        Function<Integer, Employee[]> func2 = length -> new Employee[length];
        System.out.println(func2.apply(10).length);

        // 3、数组引用的形式
        Function<Integer, Employee[]> func3 = Employee[]::new;
        System.out.println(func3.apply(10).length);

    }
}

























